package com.edu.juc.bean;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class BankAccount {
    String bankName = "中国工商银行";
    public volatile int money = 0; // 账户余额

    AtomicIntegerFieldUpdater<BankAccount> moneyUpdater =
            AtomicIntegerFieldUpdater.newUpdater(BankAccount.class, "money");

    // 存款
    public void deposit(BankAccount bankAccount, int money) {
        moneyUpdater.getAndAdd(bankAccount, money);
    }

    public void deposit(int money) {
        this.money += money;
    }

    public synchronized void withdraw(int money) {
        this.money -= money;
    }

    public synchronized int getMoney() {
        return money;
    }
}
